home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Found / FWCommon / Sources / FWPriMem.cpp next >
Encoding:
Text File  |  1996-04-25  |  4.4 KB  |  153 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPriMem.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFound.hpp"
  11.  
  12. #ifndef   FWPRIMEM_H
  13. #include "FWPriMem.h"
  14. #endif
  15.  
  16. #ifndef SLPRIDEB_H
  17. #include "SLPriDeb.h"
  18. #endif
  19.  
  20. #ifdef FW_BUILD_MAC
  21. #pragma segment FWCommon
  22. #endif
  23.  
  24. typedef void (*pfvv)();
  25. extern pfvv set_new_handler(pfvv);
  26.  
  27. //========================================================================================
  28. // set_new_handler
  29. //========================================================================================
  30.  
  31. // We provide our own definition of set_new_handler mainly as a precaution against
  32. // ending up with a shared library version of set_new_handler.  ODF as a whole has been
  33. // written assuming that operator new will throw an exception if it fails to allocate
  34. // memory.  In a runtime environment such as OpenDoc, it's possible for multiple shared
  35. // libraries to exist in once process.  An exception thrown from code in one shared
  36. // library should not propogate outside of the shared library (since there is no agreed
  37. // binary interface for exceptions across shared libraries).  This implies that if
  38. // operator new throws exceptions that operator new cannot be implemented in a shared
  39. // library.  In actuality, it's the "new hander" that throws, so there must be one new
  40. // handler per shared library, and thus one version of set_new_handler per shared library.
  41. // This unit (FWPriMem.cpp) is intended to be statically linked into each shared library
  42. // built with ODF.
  43.  
  44. static pfvv gNewHandler = 0;
  45.  
  46. #ifndef _MSC_VER
  47.  
  48. pfvv set_new_handler(pfvv handler)
  49. {
  50.     // See ARM, pp 280-81.
  51.     pfvv oldHandler = gNewHandler;
  52.     gNewHandler = handler;
  53.     return oldHandler;
  54. }
  55.  
  56. #endif
  57.  
  58. #ifdef _MSC_VER
  59.  
  60. #include <new.h>
  61.  
  62. pfvv __cdecl set_new_handler(pfvv handler)
  63. {
  64.     // See ARM, pp 280-81.
  65.     pfvv oldHandler = gNewHandler;
  66.     gNewHandler = handler;
  67.     return oldHandler;
  68. }
  69.  
  70. // [KVV] Because of the way Visual C++ 4.0 linker/library work,
  71. //    we have to define the following stuff
  72.  
  73. _PNH _set_new_handler(_PNH /* pnh */)
  74. {
  75.     return 0;
  76. }
  77.  
  78. _PNH _query_new_handler()
  79. {
  80.     return 0;
  81. }
  82.  
  83. extern "C" int _callnewh(size_t /* size */)
  84. {
  85.     return 1;
  86. }
  87.  
  88. #endif
  89.  
  90. //========================================================================================
  91. // C++ Global operator new & delete
  92. //========================================================================================
  93.  
  94. //----------------------------------------------------------------------------------------
  95. // operator new, placed memory version
  96. //----------------------------------------------------------------------------------------
  97.  
  98. // already defines placement new in <new.h>
  99. //    Turn off definition if we ought not define it
  100. #define FW_NEW_WITH_PLACEMENT
  101.  
  102. #if defined(_MSC_VER)
  103. #undef    FW_NEW_WITH_PLACEMENT
  104. #elif defined(SYMANTEC_CPLUS)
  105. #undef    FW_NEW_WITH_PLACEMENT
  106. #elif defined(__MWERKS__)
  107. #if __option(dont_inline)
  108. #undef    FW_NEW_WITH_PLACEMENT
  109. #endif
  110. #endif
  111.  
  112. #ifdef FW_NEW_WITH_PLACEMENT
  113. void *operator new(size_t /* size */,void *p)
  114. {
  115.     return p;
  116. }
  117. #endif
  118.  
  119. //----------------------------------------------------------------------------------------
  120. // operator new
  121. //----------------------------------------------------------------------------------------
  122. void *operator new(size_t size)
  123. {
  124.     void* p;
  125.     
  126.     while ((p = FW_PrimitiveAllocateBlock(size)) == 0)
  127.     {
  128.         FW_PRIV_ASSERT(gNewHandler != 0);
  129.         // ODF is written assuming that operator new will not return if it fails to
  130.         // allocate memory.  It is assumed that a newHandler will be installed which
  131.         // throws an exception.  ODF installs a newHandler in a higher layer (FWODExce)
  132.         // so a newHandler will exist unless a developer chooses to intentionally
  133.         // deinstall the newHandler by calling ::set_new_handler(0).  Doing so will
  134.         // break ODF's error handling strategy and is considered illegal.  Note
  135.         // that developers are free to install their own newHandler.
  136.         if (!gNewHandler)
  137.             return 0;
  138.         else
  139.             gNewHandler();
  140.     }
  141.  
  142.     return p;
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. // operator delete
  147. //----------------------------------------------------------------------------------------
  148. void operator delete(void *p)
  149. {
  150.     FW_PrimitiveFreeBlock(p);
  151. }
  152.  
  153.